package com.ruoyi.bl.controller;

import com.ruoyi.bl.domain.BlUser;
import com.ruoyi.bl.service.IBlUserService;
import com.ruoyi.bl.service.MemoryIdService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户Controller
 *
 * @author zhanghao
 * @date 2025-01-14
 */
@RestController
@RequestMapping("/bl/user")
public class BlUserController extends BaseController {
    @Autowired
    private IBlUserService blUserService;
    @Autowired
    private MemoryIdService MemoryIdService;

    /**
     * 查询用户列表
     */
    //@PreAuthorize("@ss.hasPermi('bl:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlUser blUser) {
        startPage();
        List<BlUser> list = blUserService.selectBlUserList(blUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    //@PreAuthorize("@ss.hasPermi('bl:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlUser blUser) {
        List<BlUser> list = blUserService.selectBlUserList(blUser);
        ExcelUtil<BlUser> util = new ExcelUtil<BlUser>(BlUser.class);
        util.exportExcel(response, list, "用户数据");
    }
    /**
     * 获取用户详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bl:user:query')")
    @GetMapping
    public AjaxResult getInfo() {
        //获取当前用户的id
        Long currentUserId = SecurityUtils.getUserId();
        //返回当前用户数据用于展示
        return success(blUserService.selectBlUserByUserId(currentUserId));
    }
    /**
     * 新增用户//注册用户
     * 此功能需要添加一个唯一的memoryid
     */
    //@PreAuthorize("@ss.hasPermi('bl:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlUser blUser) throws Exception {
        //注册用户成功
        return toAjax(blUserService.insertBlUser(blUser));
    }
    /**
     * 修改用户
     */
    //@PreAuthorize("@ss.hasPermi('bl:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlUser blUser) {
        //获取当前登录用户的id
        Long userId = SecurityUtils.getUserId();
        System.out.println("当前登录用的id为"+userId);
        blUser.setUserId(userId);
        return toAjax(blUserService.updateBlUser(blUser));
    }
    /**
     * 删除用户
     */
    //@PreAuthorize("@ss.hasPermi('bl:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(blUserService.deleteBlUserByUserIds(userIds));
    }
}
